package cn.tianyigps.tianyigpsaddress;

import cn.tianyigps.tianyigpsaddress.bean.Location;
import cn.tianyigps.tianyigpsaddress.dao.LocationDao;
import cn.tianyigps.tianyigpspublic.mongo.dao.BaseDao;
import cn.tianyigps.tianyigpspublic.util.MapTransformUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created with IDEA
 * author:guibin.cui
 * Date:2018/6/5
 * Time:11:25
 * rest 接口调用
 *
 */
@Component
@Slf4j
public class RestService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LocationDao locationDao;
    //高德地址解析接口
    public static  final  String AMAP_GEOCODE = "https://restapi.amap.com/v3/geocode/regeo?key=a041587e062315e6eaa62dd1f3fcd73d&location={lng},{lat}";
    //百度地址解析接口
    //VcegnSQh87MGVPRe52W5vB1q8KjXjG9H 甘宇
    //  企业
    //Qzw0piVh7qFDE2FPMZqkjtGi9h9qhUGI 程腾
    //gdqjDYv0uD9KHUnKMb1G2m6g05rrFZrn 赵鑫
    //ADqazQCk8WWLM9NLtXf20HXcOlo6sUB9 优装汇
    public static  final  String BAIDU_GEOCODE = "http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=0&ak=ADqazQCk8WWLM9NLtXf20HXcOlo6sUB9";

    public  Location getAmapGeocode(Double lng, Double lat) {

        Point point = MapTransformUtils.bd09togcj02(lng, lat);

        Map<String, Object> param = new HashMap<>();
        param.put("lng",point.getX());
        param.put("lat",point.getY());
        String forObject = restTemplate.getForObject(AMAP_GEOCODE, String.class, param);

        Location location = new Location();
        ObjectMapper objectMapper = new ObjectMapper();
        try {

            JsonNode jsonNode = objectMapper.readTree(forObject);
            String address  = jsonNode.get("regeocode").get("formatted_address").asText();
            if (StringUtils.isEmpty(address))
                return null;

            location.setAddress(address);
        } catch (IOException e) {
            e.printStackTrace();
        }

        DecimalFormat df = new DecimalFormat("0.0000");
        String lngStr = df.format(point.getX());
        String latStr = df.format(point.getY());

        location.setId(lngStr +","+ latStr);
        location.setLng(Double.parseDouble(lngStr));
        location.setLat(Double.parseDouble(latStr));
        return location;
    }
    public  Location getBaiduGeocode(Double lng, Double lat) {

        DecimalFormat df = new DecimalFormat("0.0000");
        String lngStr = df.format(lng);
        String latStr = df.format(lat);

        Location locationMongo = locationDao.get(lngStr + "," + latStr);
        if (Optional.ofNullable(locationMongo).isPresent())
            return locationMongo;

        Map<String, Object> param = new HashMap<>();
        param.put("lng",Double.parseDouble(lngStr));
        param.put("lat",Double.parseDouble(latStr));

        String forObject = restTemplate.getForObject(BAIDU_GEOCODE, String.class, param);
        Location location = new Location();
        ObjectMapper objectMapper = new ObjectMapper();

        try {

            JsonNode jsonNode = objectMapper.readTree(forObject);
            String address = jsonNode.get("result").get("formatted_address").asText();
            String desc  = jsonNode.get("result").get("sematic_description").asText();
            if (StringUtils.isEmpty(address))
                return null;

            location.setAddress(address);
        } catch (IOException e) {
            e.printStackTrace();
        }
        location.setId(lngStr +","+ latStr);
        location.setLng(Double.parseDouble(lngStr));
        location.setLat(Double.parseDouble(latStr));
        // 插入
        locationDao.insert(location);
        return location;
    }
}
